package de.lmu.ifi.pst.uwejsf.framework.process;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;


public class DynamicObjectUpdateHandler implements SystemActionHandler {

	String fExpression;

	public Map<String, Object> execute(Map<String, Object> parameters,
			Map<String, Object> scope) {

		Object target = parameters.get("target");
		

		for (String key : parameters.keySet()) {
			if (PropertyUtils.isWriteable(target, key)) {
				try {
					PropertyUtils.setProperty(target, key, parameters.get(key));
				} catch (Exception e) {
					throw new ProcessDataflowException("Couldn't update object: " + target.toString());
				}
			}
		}

		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", target);
		return result;

	}

	public String getExpression() {
		return fExpression;
	}

	public void setExpression(String expression) {
		fExpression = expression;

	}

}
